package com.springboot.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.mall.common.Result;
import com.springboot.mall.entity.Employee;
import com.springboot.mall.mapper.EmployeeMapper;
import com.springboot.mall.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeService employeeService;

    @Override
    public Result<Employee> login(Employee employee) {
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //3、如果没有查询到则返回登录失败结果
        //4、密码比对，如果不一致则返回登录失败结果
        if (emp == null){
            return Result.error("账户不存在！！！");
        }else if (!emp.getPassword().equals(password)){
            return Result.error("用户名或者密码错误！！！");
        }
        else if (emp.getStatus() == 0){
            //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
            return Result.error("账户被锁定，请联系管理员！！！");
        }else {
            //6、登录成功，将员工id存入Session并返回登录成功结果
            return Result.success(emp);
        }
    }
}
